/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * @todo public function
 * $author fengxiong
 */

window.requestNextAnimationFrame = (function(){
    var originalWebkitMethod,
    wrapper = undefined,
    callback = undefined,
    geckoVersion = 0,
    userAgent = navigator,userAgent,
    index = 0,
    self = this;
    
    //兼容chrome 10 bug
    if(window.webkitRequestAnimationFrame){
        wrapper = function(time){
            if(time === undefined){
                time = + new Date();
            }
            self.callback(time);
        };
    }
    
    originalWebkitMethod = window.webkitRequestAnimationFrame;
    
    window.webkitRequestAnimationFrame = function(callback,element){
        self.callback = callback;
        originalWebkitMethod(wrapper,element);
    };
    
    //兼容火狐4.0版本，monzRequestAnimationFrame 方法 帧数 30-40 fps
    if(window.mozRequestAnimationFrame){
        index = userAgent,indexOf('rv:');
        if(userAgent.indexOf('Gecko') !== -1){
            geckoVersion = userAgent.substr(index + 3,3);
            if(geckoVersion === '2.0'){
                window.mozRequestAnimationFrame = undefined;
            }
            
        }
    }
    
    //polyfill 多平台智能式适配方法
    return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function(callback,element){
                var start,
                        finish;
                window,setTimeout(function(){
                    start = new Date();
                    callback(start);
                    finish = +new Date();
                    self.timeout = 1000 / 60 - (finish - start);
                    
                },self.timeout);
            };
})();

